# Path to cross tools

MIPS_PREFIX=/it/sw/cross/mips-idt/bin/mips-idt-elf

EXECUTABLES=$(addprefix bin/, pornos)


# gcc flags for the MIPS architecture:
#  -EL     : Little endian
#  -G0     : Always use small data (using register gp)
#  -mips32 : Compile for a 32-bit MIPS architecture
#

ARCH=-EL -G0 -mips32

# Other gcc flags

CFLAGS	+= -ggdb -Wall -fno-builtin -I include -I UP

# Compiler and linker commands

CC=$(MIPS_PREFIX)-gcc

# Tell the linker where to start the text segment. 
LD=$(MIPS_PREFIX)-ld -Ttext 80020000


# Path to Simics installation

SIMICS=/home/${USER}/simics-workspace 

all: bin/pornos


#### RULE USED TO START SIMICS 

run: bin/pornos 
	./scripts/run.sh $(SIMICS) $<

#### RULE TO BUILD BINARIES FROM OBJECT FILES

bin/pornos: $(addprefix build/, boot.o pornos.o process_management.o pcb.o scheduler.o exception_handler.o kexception.o timer.o mips4kc.o delay.o syscall.o api.o user_programs.o string.o message_definition.o message.o message_queue.o message_passing.o process_supervision.o fifo_queue.o pcb_list.o uart.o display.o kprint.o malta_lcd.o dynamic_memory.o linked_list.o fibonacci.o increment.o supervised_spawn.o scroller.o shell.o supervision.o oom.o ring.o ring_node.o eat.o init.o)
	$(LD) $(ARCH) -o $@ $^

###### GENERIC BUILD PATTERNS ########

build/%.o: src/%.c
	$(CC) $(ARCH) $(CFLAGS)  -c $< -o $@	

build/%.o: src/%.S
	$(CC) $(ARCH) $(CFLAGS)  -c $< -o $@

# only for user programs
build/%.o: UP/%.c
	$(CC) $(ARCH) $(CFLAGS)  -c $< -o $@

pack: 
	tar -czvf DV_1_Project.tar.gz UP/*.c UP/*.h include/*.h src/*.c src/*.S build/*.o bin/* scripts/* documentation/*.pdf Makefile

clean: 
	pwd
	rm -f build/*.o
	rm -f include/*~ include/#* include/*#
	rm -f src/*~ src/#* src/*#
	rm -f scripts/*~ scripts/#* scripts/*#
	rm -f UP/*~
	rm -f ${EXECUTABLES}	
